﻿//* Copyright(C) 2005-2012, all rights reserved.
//*
//* The following code is copyrighted and contains proprietary information
/* and trade secrets of Foxit Software Inc.      
**************************************************************************
*/

define(function (require, exports, module) {

    var Signature_MousePtHandler = function (signaturePlugin) {
        this.signaturePlugin = signaturePlugin;
        this.signDrag = false;
    	this.signScale = false;
    	this.curMouseX;
    	this.curMouseY;
    	this.curObject;
    	this.direction;
        var _self = this;
    };
    Signature_MousePtHandler.prototype.getType = function(event){
    	return "Signature";
    };
    
    Signature_MousePtHandler.prototype.onLButtonDblClk = function (event) {
    	if(jQuery(event.target).hasClass("fwr-sign-fg")){
        	if($(event.target).parent().attr("signaturetype") == WebPDF.PDFData.SignatureType.Text){
        		$(event.target).parent().find(".fwr-sign-input").focus();
        	}
        	return true;
        }
        return false;
    };
    
    
    Signature_MousePtHandler.prototype.onLButtonDown = function (event) {
    	if(jQuery(event.target).hasClass("fwr-sign") ||jQuery(event.target).hasClass("fwr-sign-img") || jQuery(event.target).hasClass("fwr-sign-fg")){
        	 this.signDrag = true;
        	 this.curMouseX = event.pageX;
        	 this.curMouseY = event.pageY;
        	 this.curObject = jQuery(event.target).parent();
        	 this.curObject.css("cursor","move");
        	 return true;
        }else if(jQuery(event.target).hasClass("fwr-sign-scaleImg")){
        	console.log("onLbuttonDown")
        	 this.signScale = true;
        	 this.direction = jQuery(event.target).attr("direction");
        	 this.curMouseX = event.pageX;
        	 this.curMouseY = event.pageY;
        	 this.curObject = jQuery(event.target).parent();
        	 return true;
        }else if(jQuery(event.target).hasClass("fwr-sign-delImg")){
        	var signatureId = this.signaturePlugin.getSignatureDocObjIdFromObj(jQuery(event.target).parent());
        	jQuery(event.target).parent().remove();
        	this.signaturePlugin.deleteSingnatureFromPage(signatureId);
        	return true;
        }
        return false;
    };
    
    Signature_MousePtHandler.prototype.onMouseMove = function (event) {
    	var pageView;
    	if(this.signDrag || this.signScale){
    		var docView = _readerApp.getMainView().getDocView();
    		var docViewRect = docView.getDocViewClientRect();
            if (!WebPDF.RectUtils.ptInRect(docViewRect, event.pageX, event.pageY))
                return true;
        	var visiblePageRange = docView.getVisiblePageRange();
        	
        	pageViewInfo = this.signaturePlugin.getPageViewInfo(visiblePageRange,docView,event.pageX,event.pageY);
        	if(!pageViewInfo || !pageViewInfo.pageView){
        		return true;
        	}
        	pageView = pageViewInfo.pageView;
    	}
    	if(this.signDrag){
    		var idStrArray = this.curObject.attr("id").split("_");
        	var curIndex = parseInt(idStrArray[idStrArray.length - 3]);
        	var oldPageView = docView.getPageView(curIndex);
        	if(curIndex != pageView.getPageIndex()){
        		idStrArray[idStrArray.length - 3] = pageView.getPageIndex();
        		var signatureDivId = idStrArray.join("_");
	        	var divBackgroudFgId = 	signatureDivId+"_fg";
	        	
	        	var html = "";
	        	
	        	var signatureId = this.signaturePlugin.getSignatureDocObjIdFromObj(this.curObject);
	    		var oldSignatureDocObj = this.signaturePlugin.getSignatureFromDocList(curIndex,signatureId);
	    		var txt = oldSignatureDocObj.getSignatureText();
	    		var newSignatureDocObj = oldSignatureDocObj;
	    		newSignatureDocObj.setPosY(_oldPageY);
	    		
	    		var bText = this.curObject.attr("signaturetype") == WebPDF.PDFData.BaseSignature.Text;
	    		var inputId = "";
	        	if(bText){
	        		inputId = signatureDivId+"_input";
	        		this.curObject.find("textarea").attr("id",inputId);
	        	}
	        	this.curObject.attr("id",signatureDivId);
	        	this.curObject.find(".fwr-sign-fg").attr("id",divBackgroudFgId);
	        	
	        	var _oldPageY = event.pageY - pageViewRect.bottom;
	        	var h = parseInt(this.curObject.height() + 0.5);
				var maxY = pageView.getPageViewHeight()-h-12;
				_oldPageY = parseInt(_oldPageY - h / 2 + 0.5);
				if(_oldPageY < 12){
					_oldPageY = 12;
				}else if(_oldPageY > maxY){
					_oldPageY = maxY;
				}
				this.curObject.css("top",_oldPageY+"px");
	        	var html = this.curObject[0].outerHTML;
	    		this.signaturePlugin.deleteSignatureFromDocList(curIndex,signatureId);
	    		this.signaturePlugin.addSignatureToDocList(pageView.getPageIndex(),newSignatureDocObj);
	    		this.curObject.remove();
	    		$("#" + this.signaturePlugin.getSignatureContainerId(pageView)).append(html);
	    		this.curObject = $("#"+signatureDivId);
	    		this.curMouseX = event.pageX;
	    		this.curMouseY = event.pageY;
	    		if(bText){
	    			$("#" + inputId).val(txt);
	    			this.signaturePlugin.bindInputEvent(inputId);
	        	}
	    		return true;
        	}
        	
    		var offsetX = event.pageX - this.curMouseX;
    		var offsetY = event.pageY - this.curMouseY;
    		this.curMouseX = event.pageX;
    		this.curMouseY = event.pageY;
    		this.signaturePlugin.showEditSign(this.curObject,true);
    		this.signaturePlugin.moveHtmlObject(this.curObject,offsetX,offsetY,pageView);
    		return true;
    	}else if(this.signScale){
    		var offsetX = event.pageX - this.curMouseX;
    		var offsetY = event.pageY - this.curMouseY;
    		this.curMouseX = event.pageX;
    		this.curMouseY = event.pageY;
    		this.signaturePlugin.showEditSign(this.curObject,true);
    		this.signaturePlugin.changeHtmlObjct(this.curObject,offsetX,offsetY,this.direction);
    		return true;
    	}
        return false;
    };
    
    Signature_MousePtHandler.prototype.onLButtonUp = function (event) {
    	if (this.signDrag || this.signScale) {
    		//this.curObject.find(".fwr-sign-input").focus();
    		this.curObject.css("cursor","auto");
    		this.curMouseX = "";
        	this.curMouseY = "";
        	this.curObject = "";
            this.signDrag = false;
            this.signScale = false;
            this.direction = "";
    		return true;
    	}
        return false;
    };
    Signature_MousePtHandler.prototype.onMouseOver = function (event) {
    	if($(event.target).hasClass("fwr-sign-delImg") ||
    			$(event.target).hasClass("fwr-sign-scaleImg") || $(event.target).hasClass("fwr-sign-input")
    			|| $(event.target).hasClass("fwr-sign-img") || $(event.target).hasClass("fwr-signature") ||
    			$(event.target).hasClass("fwr-sign-fg")){
    		if(this.signDrag || this.signScale){
    			return true;
    		}
    		var backgroundBoolean = true;
    		
    		if($(document.activeElement).hasClass("fwr-sign-input") && 
    				($(document.activeElement).parent().attr("id") == $(event.target).parent().attr("id") || 
    						$(document.activeElement).parent().attr("id") == $(event.target).attr("id"))){
    			backgroundBoolean = false;
    		};
    		
    		if($(event.target).hasClass("fwr-signature")){
    			this.signaturePlugin.showEditSign($(event.target),backgroundBoolean);
    		}else{
    			this.signaturePlugin.showEditSign($(event.target).parent(),backgroundBoolean);
    		}
    		return true;
    	}
        return false;
    };
    Signature_MousePtHandler.prototype.onMouseOut = function (event) {
    	if($(event.target).hasClass("fwr-sign-delImg")  ||
    			$(event.target).hasClass("fwr-sign-scaleImg") || $(event.target).hasClass("fwr-sign-input")
    			|| $(event.target).hasClass("fwr-sign-img") || $(event.target).hasClass("fwr-signature") ||
    			$(event.target).hasClass("fwr-sign-fg")){

    		var object = "";
    		if($(event.target).hasClass("fwr-signature")){
    			object = $(event.target);
    		}else{
    			object = $(event.target).parent();
    		}
    		$("#"+object.attr("id")+"_fg").hide();
    		if(!($(document.activeElement).hasClass("fwr-sign-input") && 
    				($(document.activeElement).parent().attr("id") == $(event.target).parent().attr("id") || 
    						$(document.activeElement).parent().attr("id") == $(event.target).attr("id")))){
    			this.signaturePlugin.hideEditSign(object,true);
    		};
    		return true;
    	}
    	
        return false;
    };
    Signature_MousePtHandler.prototype.onRButtonDblClk = function (event) {
        return false;
    };
    Signature_MousePtHandler.prototype.onRButtonDown = function (event) {
        return false;
    };
    Signature_MousePtHandler.prototype.onRButtonUp = function (event) {
        return false;
    };
    
    Signature_MousePtHandler.prototype.onMouseWheel = function (event) {
        return false;
    };
    Signature_MousePtHandler.prototype.onMouseLeave = function (event) {

        return false;
    };
    Signature_MousePtHandler.prototype.onMouseEnter = function (event) {

    	return false;
    };
    
    
    Signature_MousePtHandler.prototype.onDoubleTap = function(event) {return false;},
    
    Signature_MousePtHandler.prototype.onPinchIn = function(event) {return false;},
    
    Signature_MousePtHandler.prototype.onPinchOut = function(event) {return false;},
   
    Signature_MousePtHandler.prototype.onHold = function(event) {return false;};
    

    WebPDF.Event.Signature_MousePtHandler = Signature_MousePtHandler;
    return WebPDF.Event.Signature_MousePtHandler;
});